package com.hehui.partimejob;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hehui.partimejob.service.model.UserModel;
import com.hehui.partimejob.service.vo.ServiceDataVo;
import com.hehui.partimejob.items.body.UserBean;
import com.hehui.partimejob.network.MyNetwork;
import com.hehui.partimejob.network.OnNetworkListener;
import com.hehui.partimejob.uitls.DisplayUtil;
import com.hehui.partimejob.uitls.MyUtil;
import com.hehui.partimejob.view.InputView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RegisterActivity extends BaseActivity {
    private LinearLayout container;
    private TextView registerBtn;
    private Handler mHandler;
    private double verifyCodeId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getNavView().setTitle(getString(R.string.goRegister));


        ArrayList<InputView.InputData> inputData = new ArrayList<>();
        inputData.add(new InputView.InputData().setHint(getString(R.string.email_hint)).setIcon(R.drawable.email).setId(View.generateViewId()));
        inputData.add(new InputView.InputData().setHint(getString(R.string.verifycode_hint)).setIcon(R.drawable.code).setId(View.generateViewId()));
        ArrayList<InputView.InputData> inputData2 = new ArrayList<>();
        inputData2.add(new InputView.InputData().setHint(getString(R.string.username_hint)).setIcon(R.drawable.username).setId(View.generateViewId()));
        inputData2.add(new InputView.InputData().setHint(getString(R.string.password_hint)).setIcon(R.drawable.pwd).setId(View.generateViewId()));
        inputData2.add(new InputView.InputData().setHint(getString(R.string.passwordtwice_hint)).setIcon(R.drawable.pwd).setId(View.generateViewId()));
        InputView inputView = new InputView();
        InputView inputView2 = new InputView();
        List<InputView.Item> items = inputView.addInputView(this, inputData);
        List<InputView.Item> items1 = inputView2.addInputView(this, inputData2);
        container.addView(inputView.getmContainer(), 1);
//        清除上边距
        LinearLayout linearLayout = inputView2.getmContainer();
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
        layoutParams.topMargin = DisplayUtil.dp2px(this, -13);
        linearLayout.setLayoutParams(layoutParams);
        container.addView(linearLayout, 2);
//添加验证码按钮
        ViewGroup parent = (ViewGroup) items.get(1).getEditText().getParent();
        TextView verify = new TextView(this);
        verify.setText(getString(R.string.send));
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(DisplayUtil.dp2px(this, 65), DisplayUtil.dp2px(this, 40));
        layoutParams1.gravity = Gravity.CENTER | Gravity.RIGHT;
        layoutParams1.rightMargin = DisplayUtil.dp2px(this, 16);
        verify.setLayoutParams(layoutParams1);
        verify.setTextColor(getColor(R.color.white));
        verify.setTextSize(20);
        verify.setGravity(Gravity.CENTER);
        verify.setBackgroundResource(R.drawable.btn_bg);
        parent.addView(verify, parent.getChildCount());
        items.get(0).getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        items1.get(1).getEditText().setTransformationMethod(PasswordTransformationMethod.getInstance());
        items1.get(2).getEditText().setTransformationMethod(PasswordTransformationMethod.getInstance());
/**
 * 验证码功能
 */

        mHandler = new Handler(Looper.getMainLooper());
        verify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = items.get(0).getValue();
                if ("".equals(phone)) {
                    Toast.makeText(RegisterActivity.this, getString(R.string.email_hint), Toast.LENGTH_SHORT).show();
                    return;
                }
//               发送验证码
                MyNetwork.getInstance(A).get("/email/qq?email=" + phone, new OnNetworkListener<Double>() {
                    @Override
                    public void handleData(ServiceDataVo<Double> data) {
                        Double codeId = data.getData();
                        Log.d(TAG, "验证码ID: " + codeId);
                        verifyCodeId = codeId;

                    }
                });


                mHandler.post(verifyTask);
                verify.setEnabled(false);


            }

            private int VERIFY_TIME = 5;
            private final Runnable verifyTask = new Runnable() {
                @Override
                public void run() {
                    if (VERIFY_TIME <= 0) {
                        VERIFY_TIME = 5;
                        verify.setText(getString(R.string.send));
                        verify.setEnabled(true);
                        mHandler.removeCallbacks(this);
                        return;
                    }
                    int VERIFY_DELAY = 1000;
                    mHandler.postDelayed(this, VERIFY_DELAY);
                    verify.setText(String.valueOf(VERIFY_TIME));
                    VERIFY_TIME--;
                }
            };
        });

/**
 * 注册功能
 */

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = items.get(0).getEditText().getText().toString();
                String code = items.get(1).getEditText().getText().toString();
                String username = items1.get(0).getEditText().getText().toString();
                String password = items1.get(1).getEditText().getText().toString();
                String password2 = items1.get(2).getEditText().getText().toString();
                if (!MyUtil.checkHasValue(
                        A,
                        email,
                        code,
                        username,
                        password,
                        password2
                )) {
                    return;
                }
                if (!password.equals(password2)) {
                    Toast.makeText(A, "两次密码不一致！", Toast.LENGTH_SHORT).show();
                    return;
                }
                UserBean userBean = new UserBean();
                userBean.setUsername(username);
                userBean.setPassword(password);
                userBean.setEmail(email);
                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                stringObjectHashMap.put("user", new Gson().toJson(userBean));
                stringObjectHashMap.put("cid", verifyCodeId);
                stringObjectHashMap.put("code", code);
                MyNetwork.getInstance(A).post("/user/register", stringObjectHashMap, new OnNetworkListener<UserModel>() {
                    @Override
                    public void handleData(ServiceDataVo<UserModel> data) {
                        UserModel data1 = data.getData();
                        if (data1 == null) {
                            Toast.makeText(A, "出现了一点问题", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Toast.makeText(A, "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(LoginActivity.class);
                    }


                });


            }
        });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void init() {
        container = (LinearLayout) findViewById(R.id.container);
        registerBtn = (TextView) findViewById(R.id.register);


    }
}